﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 读取西门子PLC_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Timer.Tick += Timer_Tick;
        }

        private Timer Timer = new Timer();
        private SiemensClient siemensClient;

        private void button1_Click(object sender, EventArgs e)
        {
            siemensClient?.Disconnect();
            siemensClient = new SiemensClient();
            siemensClient.IpAddress = "192.168.0.120";
            siemensClient.Port = 102;
            var result = siemensClient.Connect();
            if (result.IsSucceed)
            {
                MessageBox.Show("连接成功");
            }
            else
            {
                MessageBox.Show(result.Err);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Timer.Enabled = !Timer.Enabled;
        }

  

        private void Timer_Tick(object sender, EventArgs e)
        {
            txt_bool.Text = siemensClient.Read("db7.dbx0.0", DataTypeEnum.Bool).ToString();
            txt_int.Text = siemensClient.Read("db7.dbw2", DataTypeEnum.Int16).ToString();
            txt_word.Text = siemensClient.Read("db7.dbw4", DataTypeEnum.Int16).ToString();
            txt_dword.Text = siemensClient.Read("db7.dbw6", DataTypeEnum.Int32).ToString();
            txt_real.Text = siemensClient.Read("db7.dbd10", DataTypeEnum.Float).ToString();
        }
    }
}